﻿Imports UnScripter.Project

' The Main Window for the IDE where most of the work is done
Public Class MainForm

    ' Context menu strip
    Public Property EditorTabMenuStrip As TabContextMenu

    ' Manages editor tabs separately from the tab control
    Public Property EditorTabManager As EditorTabManager

    ' Event handlers
    '   Event handlers are manually set (not using the winforms designer)
    '   for the reason of having better namespacing

    Private Property FileMenu As New FileMenu
    Private Sub AddFileMenuHandlers()
        ' Set all the event handles for the "File->" menu
        AddHandler Me.FileToolStripMenuItem.DropDownOpening, AddressOf FileMenu.FileMenu_DropDown

        AddHandler Me.ProjectNewToolStripMenuItem.Click, AddressOf FileMenu.ProjectNewToolStripMenuItem_Click
        AddHandler Me.ProjectOpenToolStripMenuItem.Click, AddressOf FileMenu.ProjectOpenToolStripMenuItem_Click
        AddHandler Me.ProjectSaveMenuItem.Click, AddressOf FileMenu.ProjectSaveToolStripMenuItem_Click
        AddHandler Me.NewToolStripMenuItem.Click, AddressOf FileMenu.NewToolStripButton_Click
        AddHandler Me.OpenToolStripMenuItem.Click, AddressOf FileMenu.OpenToolStripButton_Click
        AddHandler Me.SaveToolStripMenuItem.Click, AddressOf FileMenu.SaveToolStripMenuItem_Click
        AddHandler Me.SaveAllToolStripMenuItem.Click, AddressOf FileMenu.SaveAllToolStripMenuItem_Click
        AddHandler Me.PrintToolStripMenuItem.Click, AddressOf FileMenu.PrintToolStripMenuItem_Click
        AddHandler Me.PrintPreviewToolStripMenuItem.Click, AddressOf FileMenu.PrintPreviewToolStripMenuItem_Click
        AddHandler Me.ExitToolStripMenuItem.Click, AddressOf FileMenu.ExitToolStripMenuItem_Click
    End Sub

    Private Property EditMenu As New EditMenu
    Private Sub AddEditMenuHandlers()
        AddHandler Me.EditToolStripMenuItem.DropDownOpening, AddressOf EditMenu.EditMenu_DropDown

        AddHandler Me.UndoToolStripMenuItem.Click, AddressOf EditMenu.UndoToolStripMenuItem_Click
        AddHandler Me.RedoToolStripMenuItem.Click, AddressOf EditMenu.RedoToolStripMenuItem_Click
        AddHandler Me.CutToolStripMenuItem.Click, AddressOf EditMenu.CutToolStripMenuItem_Click
        AddHandler Me.CopyToolStripMenuItem.Click, AddressOf EditMenu.CopyToolStripMenuItem_Click
        AddHandler Me.PasteToolStripMenuItem.Click, AddressOf EditMenu.PasteToolStripMenuItem_Click
        AddHandler Me.FindToolStripMenuItem.Click, AddressOf EditMenu.FindToolStripMenuItem_Click
        AddHandler Me.FindInFilesToolStripMenuItem.Click, AddressOf EditMenu.FindFilesToolStripMenuItem_Click
        AddHandler Me.ReplaceToolStripMenuItem.Click, AddressOf EditMenu.ReplaceToolStripMenuItem_Click
        AddHandler Me.ReplaceInFilesToolStripMenuItem.Click, AddressOf EditMenu.ReplaceFilesToolStripMenuItem_Click
        AddHandler Me.SelectAllToolStripMenuItem.Click, AddressOf EditMenu.SelectAllToolStripMenuItem_Click

        AddHandler Me.GoToParentToolStripMenuItem.Click, AddressOf EditMenu.GotoParentClassToolStripMenuItem_Click
    End Sub

    Private Property BuildMenu As New BuildMenu
    Private Sub AddBuildMenuHandlers()
        AddHandler Me.BuildToolStripMenuItem.DropDownOpening, AddressOf BuildMenu.BuildMenu_DropDown

        AddHandler Me.BuildAllToolStripMenuItem.Click, AddressOf BuildMenu.BuildAllToolStripMenuItem_Click
        AddHandler Me.BuildAndRunToolStripMenuItem.Click, AddressOf BuildMenu.BuildAndRunToolStripMenuItem_Click
        AddHandler Me.RunToolStripMenuItem.Click, AddressOf BuildMenu.RunToolStripMenuItem_Click
        AddHandler Me.RebuildToolStripMenuItem.Click, AddressOf BuildMenu.BuildFullToolStripMenuItem_Click

        AddHandler Me.ErrorNextToolStripMenuItem.Click, AddressOf BuildMenu.RunToolStripMenuItem_Click
    End Sub

    Private Property ExternalMenu As New ExternalMenu
    Private Sub AddExternalMenuHandlers()
        AddHandler Me.UnrealEditorToolStripMenuItem.Click, AddressOf ExternalMenu.UnrealEditorToolStripMenuItem_Click
        AddHandler Me.UnrealLocalizerToolStripMenuItem.Click, AddressOf ExternalMenu.UnrealLocalizerToolStripMenuItem_Click
        AddHandler Me.UnrealFrontendToolStripMenuItem.Click, AddressOf ExternalMenu.UnrealFrontendToolStripMenuItem_Click
        AddHandler Me.OpenConfigFolderToolStripMenuItem.Click, AddressOf ExternalMenu.OpenConfigFolderToolStripMenuItem_Click
        AddHandler Me.OpenExplorerToolStripMenuItem.Click, AddressOf ExternalMenu.OpenExplorerToolStripMenuItem_Click
        AddHandler Me.OpenTerminalToolStripMenuItem.Click, AddressOf ExternalMenu.OpenTerminalToolStripMenuItem_Click
    End Sub

    Private Property ToolsMenu As New ToolsMenu
    Private Sub AddToolsMenuHandlers()
        AddHandler Me.ShowResourcesToolStripMenuItem.Click, AddressOf ToolsMenu.ShowResourcesToolStripMenuItem_Click
        AddHandler Me.PreferencesToolStripMenuItem.Click, AddressOf ToolsMenu.ShowOptions
    End Sub

    Private Property WindowsMenu As New WindowsMenu
    Private Sub AddWindowsMenuHandlers()
        AddHandler WindowsToolStripMenuItem.DropDownOpening, AddressOf WindowsMenu.WindowsToolStripMenuItem_DropDownOpened

        AddHandler Me.FocusEditorToolStripMenuItem.Click, AddressOf WindowsMenu.FocusEditor
        AddHandler Me.ShowClassBrowserToolStripMenuItem.Click, AddressOf WindowsMenu.ShowClassBrowser
        AddHandler Me.ProjectViewToolStripMenuItem.Click, AddressOf WindowsMenu.ShowFileExplorer
        AddHandler Me.ErrorViewToolStripMenuItem.Click, AddressOf WindowsMenu.ShowErrorView
        AddHandler Me.ConsoleOutputToolStripMenuItem.Click, AddressOf WindowsMenu.ShowConsoleOutput
        AddHandler Me.CloseToolStripMenuItem.Click, AddressOf WindowsMenu.Close
        AddHandler Me.CloseOthersToolStripMenuItem.Click, AddressOf WindowsMenu.CloseOthers
        AddHandler Me.CloseAllToolStripMenuItem.Click, AddressOf WindowsMenu.CloseAll
    End Sub

    Private Property AboutMenu As New AboutMenu
    Private Sub AddAboutMenuHandlers()
        AddHandler Me.AboutToolStripMenuItem.Click, AddressOf AboutMenu.ShowAboutBox
    End Sub

    Private Sub AddToolStripHandlers()
        AddHandler Me.ProjectNewToolStripButton.Click, AddressOf FileMenu.ProjectNewToolStripMenuItem_Click
        AddHandler Me.ProjectOpenToolStripButton.Click, AddressOf FileMenu.ProjectOpenToolStripMenuItem_Click
        AddHandler Me.ProjectSaveToolStripButton.Click, AddressOf FileMenu.ProjectSaveToolStripMenuItem_Click
        AddHandler Me.NewToolStripButton.Click, AddressOf FileMenu.NewToolStripButton_Click
        AddHandler Me.OpenToolStripButton.Click, AddressOf FileMenu.OpenToolStripButton_Click
        AddHandler Me.SaveToolStripButton.Click, AddressOf FileMenu.SaveToolStripMenuItem_Click
        AddHandler Me.SaveAllToolStripButton.Click, AddressOf FileMenu.SaveAllToolStripMenuItem_Click
        AddHandler Me.PreferencesToolStripButton.Click, AddressOf ToolsMenu.ShowOptions
        AddHandler Me.ShowResDialogToolStripButton.Click, AddressOf ToolsMenu.ShowResourcesToolStripMenuItem_Click
    End Sub

    Private Property ControlEvents As New ControlEvents
    Private Sub AddControlEvents()
        ' Add events handlers for controls inside of mainforms
        AddHandler EditorTabs.KeyDown, AddressOf ControlEvents.EditorTabs_KeyDown
        AddHandler EditorTabs.MouseClick, AddressOf ControlEvents.EditorTabs_MouseClick

        AddHandler FileView.KeyDown, AddressOf ControlEvents.FileView_KeyDown
        AddHandler FileView.NodeMouseClick, AddressOf ControlEvents.FileView_MouseClick
        AddHandler FileView.NodeMouseDoubleClick, AddressOf ControlEvents.FileView_NodeMouseDoubleClick
    End Sub

    Private Property BackgroundWorkers As New BackgroundWorkers
    Private Sub AddBackgroundWorkers()
        AddHandler Me.BuildWorker.DoWork, AddressOf BackgroundWorkers.BuildWorker_DoWork
        AddHandler Me.BuildWorker.RunWorkerCompleted, AddressOf BackgroundWorkers.BuildWorker_RunWorkerCompleted
        AddHandler Me.UnrealParserWorker.DoWork, AddressOf BackgroundWorkers.UnrealParserWorker_DoWork
        AddHandler Me.UnrealParserWorker.RunWorkerCompleted, AddressOf BackgroundWorkers.UnrealParserWorker_RunWorkerCompleted
    End Sub

    Private Sub AddEventHandlers()
        ' Setup the event handlers
        AddFileMenuHandlers()
        AddEditMenuHandlers()
        AddBuildMenuHandlers()
        AddExternalMenuHandlers()
        AddToolsMenuHandlers()
        AddWindowsMenuHandlers()
        AddAboutMenuHandlers()
        AddToolStripHandlers()
        AddControlEvents()
        AddBackgroundWorkers()
    End Sub

    ' Main
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Text = Globals.kApplicationName

        ' TODO: Settings initialization somewhere else
        ' Create and Load the global settings
        Globals.Settings = New Settings("xml/global_settings.xml", "GlobalSettings")
        Globals.EditorSettings = New Settings("xml/editor_settings.xml", "EditorSettings")
        Globals.UISettings = New Settings("xml/ui_settings.xml", "UISettings")

        ' Write settings to disk on every change
        Globals.Settings.ImmediateWrite = True
        Globals.EditorSettings.ImmediateWrite = True
        Globals.UISettings.ImmediateWrite = True

        EditorTabManager = New EditorTabManager(EditorTabs)
        EditorTabMenuStrip = New TabContextMenu(EditorTabManager)

        ' We'll set everything at the end to avoid segment faults
        AddEventHandlers()

        ' TODO: Move somewhere else
        Dim lastproject = Globals.Settings.GetTrait("LastProjectPath")
        If IO.File.Exists(lastproject) Then
            Globals.CurrentProject = ProjectManager.OpenProject(lastproject)
        End If

        ' TODO: Move somewhere else...
        ' Load the settings for the file explorer
        Dim fileviewtypestring As String =
            Globals.UISettings.GetTrait("FileViewType", "CONTEMPORARY")

        Dim fileviewparsed As FileView.FileViewMode
        [Enum].TryParse(Of FileView.FileViewMode)(fileviewtypestring, fileviewparsed)

        FileView.FileViewType = fileviewparsed

        Docks.LoadDockSettings()
        Docks.ShowDefaultDocks()

    End Sub

    Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Docks.SaveDockSettings()
    End Sub

    ' TODO: Move to globals
    Public Sub StartParser()
        ParserStatusLabel.Text = "Parsing UnrealScript..."
        ParserStatusProgressBar.Visible = True
        ParserStatusProgressBar.Value = 10
        UnrealParserWorker.RunWorkerAsync()
    End Sub

    Public Property Docks As New MainFormDocks()

    ReadOnly Property ErrorView As ErrorView
        Get
            Return Docks.ErrorViewDock.ErrorView
        End Get
    End Property

    ReadOnly Property ConsoleOutput As ConsoleOutput
        Get
            Return Docks.ConsoleOutputDock.ConsoleOutput
        End Get
    End Property

    ReadOnly Property FileView As FileView
        Get
            Return Docks.FileViewDock.FileView
        End Get
    End Property

    ReadOnly Property EditorTabs As EditorTabs
        Get
            Return Docks.EditorTabDock.EditorTabs
        End Get
    End Property

    ReadOnly Property ClassView As ClassView
        Get
            Return Docks.ClassBrowserDock.ClassView
        End Get
    End Property

    Private Sub HideEditorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Docks.EditorTabDock.Visible Then
            Docks.EditorTabDock.Hide()
        Else
            Docks.EditorTabDock.Show(DockPanel)
        End If
    End Sub
End Class
